#coding=utf-8

from django.http import Http404
#from django.conf import settings as _settings
from seblog import settings as _settings
#from safecenter.business_logic.comm.permission import UserManager
#from safecenter.common_logic.util import exception_mgr

def settings(request):
    r_dic = {'settings' : {'urlprefix' : _settings.URL_PREFIX} }
    session_key = request.COOKIES.get('session_key', None)
    userid = None
    try :
#        userid = UserManager.get_userid_by_session(session_key)
        userid =1
    except Exception, e:
        print e
    if userid is None: userid = ''
    r_dic['settings']['ruser'] = userid
    return r_dic

class CustomMiddleware(object):
    def process_request(self, request):
        if not hasattr(request, 'userid'):
            session_key = request.COOKIES.get('session_key', None)
            userid = None
            try :
#                userid = UserManager.get_userid_by_session(session_key)
                userid =1
            except:
                pass
            if userid is None: userid = 'Anonymous'
            setattr(request, 'userid', userid)
        return None
    
    def process_exception(self, request, e):
        """处理请求的参数异常，如果是其他异常，则记录到日志文件"""
        if not isinstance(e, Http404): # 404错误不记录
            pass
#            exception_mgr.on_except(None, 1)
        return None